﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using MapControl.Misc;
using MapLib;

using Point = MapWinGIS.Point;

namespace MapControl
{
    public class Coordinate9x9
    {
        private double NWLon;
        private double NWLat;

        private double SELon;
        private double SELat;

        //private AxMapXLib.AxMap AxMap1;
        private ucMap objMapControl;

        //Big BorderSquare
        public SquareCoord bigSquare;
        //Basic BorderSquare
        public SquareCoord basicSquare;
        //Medium BorderSquare
        public SquareCoord mediumSquare;
        //Imagine BorderSquare
        public SquareCoord imagineSquare;


        //Mảng 2 chiều lưu các index của bigsquare trong tọa độ 9x9
        private string[] _bigSquareArray;
        public string[] BigSquareArray
        {
            get
            {
                return _bigSquareArray;
            }
            set { _bigSquareArray = value; }
        }

        public void Init()
        {
            //North West
            NWLon = (double)97;
            NWLat = (double)30;

            //South East
            SELon = (double)117;
            SELat = (double)5;

            double x1, y1, x2, y2;
            x1 = y1 = x2 = y2 = 0;

            //Convert to Screen Coord
            objMapControl.AxMap1.ProjToPixel(NWLon, NWLat, ref x1, ref y1);
            objMapControl.AxMap1.ProjToPixel(SELon, SELat, ref x2, ref y2);

            double dX = Math.Abs(x2 - x1);
            double dY = Math.Abs(y1 - y2);

            //Big Square
            RectangleF rect = new RectangleF((float)x1, (float)y1, (float)dX, (float)dY);
            bigSquare = new BigSquareCoord(rect, 2,5);
            bigSquare.BigSquareArray = BigSquareArray;

            //Basic Square
            rect = new RectangleF(0, 0, bigSquare.Width, bigSquare.Height);
            basicSquare = new BasicSquareCoord(rect, 10,10, bigSquare);

            //Medium Squares
            rect = new RectangleF(0, 0, basicSquare.Width, basicSquare.Height);
            mediumSquare = new MediumSquareCoord(rect,3,3, basicSquare);

            //Imagine Square
            rect = new RectangleF(0, 0, mediumSquare.Width, mediumSquare.Height);
            imagineSquare = new ImagineSquareCoord(rect,3,3, mediumSquare);
        }

        public Coordinate9x9(ucMap map)
        {
            objMapControl = map;
        }

        /// <summary>
        /// Draw rectangle
        /// </summary>
        /// <param name="g"></param>
        public void Draw(Graphics g)
        {
            //Init();
            Pen pen = new Pen(Color.Red);
            pen.Dispose();
            float width = 4;
            if (objMapControl.Altitude < 4000)
            {
                width = 2;
            }
            else if (objMapControl.Altitude < 5000)
            {
                width = 1;
            }
            else if (objMapControl.Altitude < 6000)
            {
                width = (float)0.5;
            }
            else if (objMapControl.Altitude < 7000)
            {
                width = (float)0.25;
            }
           
            //Dựa vào tỉ lệ zoom để vẽ lưới 9x9
            if (objMapControl.Altitude < 1000)
            {
                //Vẽ ô vuông tưởng tượng
                imagineSquare.Draw(g, width/4);
            }

            if (objMapControl.Altitude < 2000)
            {
                //Vẽ ô vuông vừa 
                mediumSquare.Draw(g, width / 3);
            }

            if (objMapControl.Altitude < 3000)
            {
                //Vẽ ô vuông cơ bản
                basicSquare.Draw(g, width / 2);
            }
            if (objMapControl.Altitude < 7000)
            {
                //Vẽ ô vuông lớn
                bigSquare.Draw(g, width);
            }
            else if (objMapControl.Altitude < 10000)
            {
                //Vẽ ô vuông lớn
                bigSquare.Draw(g, 1);
            }

        }

        /// <summary>
        /// Xác định tọa độ tại vị trí mouse hiện tại
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public string GetCoordinate(PointF p)
        {
            string resultCoord = "";
            if (bigSquare.BorderSquare.Contains(p)){
                //resultCoord = string.Format("BigSquare:{0} - BasicSquare:{1} - MediumSquare:{2} - ImagineSquare:{3}", bigSquare.GetCoord(p), basicSquare.GetCoord(p), mediumSquare.GetCoord(p), imagineSquare.GetCoord(p));
                resultCoord = string.Format("{0}{1}{2}{3}", bigSquare.GetCoord(p), basicSquare.GetCoord(p), mediumSquare.GetCoord(p), imagineSquare.GetCoord(p));
            }
            return resultCoord;
        }
    }
}
